<?php


	include_once 'json_function.php';
	$ini_array = parse_ini_file("config/config.ini");
	
	$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
	mysqli_set_charset($conn, "utf8");
	ini_set('default_charset', "UTF-8");
	
	$appointmentNo = mysqli_real_escape_string($conn,$_POST["appointmentNo"]);
	$patientNo = mysqli_real_escape_string($conn,$_POST["patientNo"]);
	$dateVisit = mysqli_real_escape_string($conn,$_POST["dateVisit"]);
	$clinicNo = mysqli_real_escape_string($conn,$_POST["clinicNo"]);
	$isPrinted = $_POST["isPrinted"];
	$isFirstVisit = $_POST["isFirstVisit"];
	$signs = mysqli_real_escape_string($conn,$_POST["signs"]);
	$symptoms = mysqli_real_escape_string($conn,$_POST["symptoms"]);
	$diagnosis = mysqli_real_escape_string($conn,$_POST["diagnosis"]);
	$treatment = mysqli_real_escape_string($conn,$_POST["treatment"]);
	$comments = mysqli_real_escape_string($conn,$_POST["comments"]);
	$answerQuestion = mysqli_real_escape_string($conn,$_POST["answerQuestion"]);
	$professionalFee = mysqli_real_escape_string($conn,$_POST["professionalFee"]);
	if( strcmp($professionalFee,"") == 0 ){
		$professionalFee = 0;
	}
	$paymentMethod = mysqli_real_escape_string($conn,$_POST["paymentMethod"]);
	$isPayed = 0;
	if( isset($_POST["isPayed"])){
		$isPayed = $_POST["isPayed"];
	}
	$procedureNo = mysqli_real_escape_string($conn,$_POST["procedure"]);
	$ICD9CodeNo = mysqli_real_escape_string($conn,$_POST["ICD9Code"]);
	
	
	
	$existSql = "select count(*) count from visit where Appointment_no=".$appointmentNo;
	$existResult = mysqli_query($conn, $existSql);
	$count = 0;
	if( $row = mysqli_fetch_array($existResult)){
		$count = $row["count"];
	}
	
	//opereate type: add or update
	$operateType = "Update";
	
	if( $count == 0 ){
		$insertSql = "insert into visit(Appointment_no)values(".$appointmentNo.")";
		mysqli_query($conn, $insertSql);
		$operateType = "Add";
		
	}
	
	$updateSql = "update visit set Patient_no=".$patientNo.", Datetime_patient_seen=str_to_date('".$dateVisit."','%d/%m/%Y %H:%i'), Clinic_no=".$clinicNo.
	", is_printed=".$isPrinted.", signs='".$signs."',symptoms='".$symptoms."', diagnosis='".$diagnosis."', treatment='".$treatment.
	"', comments='".$comments."', answer_to_questionaire='".$answerQuestion."',Professional_fee=".$professionalFee.", isPay=".$isPayed.", payment_method='".$paymentMethod.
	"'";  
	
	if( strcmp($procedureNo,"") != 0 && $procedureNo != 0){
		$updateSql .= ",Procedure_no=".$procedureNo;
	}else{
		$updateSql .= ",Procedure_no=null";
	}
	
	
	if( strcmp($ICD9CodeNo,"") != 0 && $ICD9CodeNo != 0 ){
		$updateSql = $updateSql.", ICD9Code_no=".$ICD9CodeNo;
	}else{
		$updateSql = $updateSql.", ICD9Code_no=null";
	}           
	
	$updateSql .= " where appointment_no=".$appointmentNo;
	$updateResult = mysqli_query($conn, $updateSql);
	//echo $updateSql;
	//echo mysql_error();
	
	if( $updateResult && $count == 0 ){
		$updateAppSql = "update appointment set type=3 where appointment_no=".$appointmentNo;
		$updateResult = mysqli_query($conn, $updateAppSql);
	}
	
	//if( $updateResult && $isFirstVisit == 1 ){
	//	$updatePatientSql = "update patient set isFirstVisit=0 where patient_no=".$patientNo;
	//	$updateResult = mysqli_query($conn, $updatePatientSql);
	//}
	
	$data = "{\"data\":{";
	if( $updateResult ){
		$data .= "\"status\":\"success\",\"msg\":\"".$operateType." review info  success!!!\"}}";
	}else{
		$data .= "\"status\":\"fail\",\"msg\":\"".$operateType." review info fail!!! ".mysqli_error($conn)."\"}}";
	}
	
	echo JsonCharFilter($data);
	
	



?>
